package com.sicilin.common.page;

import com.sicilin.common.json.datatable.DataTableRequest;
import com.sicilin.common.json.datatable.DataTableResponse;
import com.sicilin.common.json.date.DateUtils;
import com.sicilin.common.json.easyui.vo.DatagridResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by sicilin on 2017/7/31.
 */
public class PageUtils {

    public static PageObject initPageObject(HttpServletRequest request){
        PageObject pageObject = new PageObject(50);
        if(request != null){
            String rowsStr = request.getParameter("rows");
            String pageStr = request.getParameter("page");
            if(StringUtils.isNotBlank(rowsStr) && StringUtils.isNumeric(rowsStr) ){
                pageObject.setRows(Integer.valueOf(rowsStr));
            }
            if(StringUtils.isNotBlank(pageStr) && StringUtils.isNumeric(pageStr) ){
                pageObject.setPage(Integer.valueOf(pageStr));
            }
        }

        return pageObject;
    }

    public static PageObject initPageObject(DataTableRequest request){
        PageObject pageObject = new PageObject(50);
        if(request != null){
            int start = request.getStart() != null?request.getStart():0;
            int page = request.getLength() != null? start/request.getLength() + 1 : 1;
            if(request.getLength() != null) {
                pageObject.setRows(request.getLength());
            }
            pageObject.setPage(page);
        }
        return pageObject;
    }


    /**
     * 默认将常用的时间类 转成'yyyy-MM-dd HH:mm:ss'的格式<br>
     *     转换的时间类：java.util.Date  java.sql.Date  java.sql.Timestamp
     * @param pageObject
     * @return
     */
    public static JSONObject ToEasyUIDatagridJSON(PageObject pageObject){

        return  ToEasyUIDatagridJSON(pageObject, DateUtils.getJsDateJSONConfig());
    }

    public static JSONObject ToEasyUIDatagridJSON(PageObject pageObject,JsonConfig jsonConfig){
        JSONObject result = new JSONObject();
        JSONArray jsonArr = JSONArray.fromObject(pageObject.getRowsList(),jsonConfig);
        result.put("rows",jsonArr);
        result.put("total",pageObject.getTotal());
        return  result;
    }

    public static DatagridResponse ToEasyUIDatagrid(PageObject pageObject){
        DatagridResponse response = new DatagridResponse(pageObject.getTotal(),pageObject.getRowsList());
        return response;
    }
    public static DataTableResponse toDataTables(PageObject pageObject,int draw){
        DataTableResponse response = new DataTableResponse();
        response.setDraw(draw);
        response.setRecordsTotal(pageObject.getTotal());
        response.setRecordsFiltered(pageObject.getRows());
        response.setData(pageObject.getRowsList());
        return response;
    }

    public static int getMaxPage(int total,int pageSize){
        return total%pageSize == 0 ? total/pageSize : total/pageSize+1 ;
    }
    public static int getNumberStart(int page, int pageSize){
        return pageSize*(page-1);
    }

    public static int getNumberStart(PageObject pageObject){
        if(pageObject == null)return 0;
        return getNumberStart(pageObject.getPage(),pageObject.getRows());
    }

    public static int getMaxPage(PageObject pageObject){
        if(pageObject == null)return 0;
        return getMaxPage(pageObject.getTotal(),pageObject.getRows());
    }
}
